import json
import threading, os
from django.http import HttpResponse
from django.shortcuts import render
from django.views import View
from funboost.timing_job.apscheduler_use_redis_store import funboost_background_scheduler_redis_store
# from test_frame.test_apschedual.test_aps_redis_store import consume_func
# from utils.test_aps_redis_store import consume_func
# from utils.test_change_aps_conf import update_create_aps


class HelloView(View):
    @staticmethod
    def get(requests):
        data = {
            "code": 200,
            "data": "hello tang"
        }

        json_data = json.dumps(data)
        # print(f'dumps：{json_data}, type：{type(json_data)}')
        loads_data = json.loads(json_data)
        # print(f'loads：{loads_data}，type: {type(loads_data)}')
        return HttpResponse(json_data, content_type="application/json")



# class ApsView(View):
#     def get(self, request):
#         # 这里的进程不再重要，只要把参数给funboost处理即可，定时任务存储在redis中
#         t = threading.currentThread()
#         pid = os.getpid()
#         print(f"修改进程id是：{pid}")
#         print(f"修改线程id是：{t.ident}")
#         data = request.body
#         data = json.loads(data.decode("utf-8"))
#         id = data.get('id')
#         seconds = data.get('seconds')
#         kwargs = data.get('kwargs')
#
#         # 修改或添加定时任务必须执行.start，值修改配置但不想执行函数，就设置paused=True
#         # funboost_background_scheduler_redis_store.start(
#         #     paused=True)  # 此脚本是演示远程动态修改的脚本，动态新增或删除定时任务配置，但不执行定时任务执行。注意是 paused=True
#
#         # 增加或修改定时任务配置 replace_existing=True 就是如果id已存在就修改，不存在则添加。
#         # funboost_background_scheduler_redis_store.add_push_job(consume_func,
#         #                                                        'interval', id='15', name='namexx', seconds=10,
#         #                                                        kwargs={"x": 901111, "y": 90},
#         #                                                        replace_existing=True)
#         result = update_create_aps(id, seconds, kwargs)
#         data = {
#             "code": 200,
#             "data": f"{result}"
#         }
#         json_data = json.dumps(data)
#         return HttpResponse(json_data, content_type="application/json")